package org.example.design.rabbit.work;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.example.design.rabbit.constant.Constants;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class ProducerDemo {
    public static void main(String[] args) throws IOException, TimeoutException {
        //1.创建连接
        ConnectionFactory connectionFactory=new ConnectionFactory();
        connectionFactory.setHost(Constants.HOST);
        connectionFactory.setPort(Constants.PORT);
        connectionFactory.setUsername(Constants.USER_NAME);
        connectionFactory.setPassword(Constants.PASSWORD);
        connectionFactory.setVirtualHost(Constants.VIRTUAL_HOST);
        Connection connection=connectionFactory.newConnection();

        //2.开启信道
        Channel channel=connection.createChannel();

        //3.声明交换机,声明队列
        channel.queueDeclare(Constants.WORK_QUEUE,true,false,false,null);

        //4.发送消息
        String msg="hello work queue";
        channel.basicPublish("",Constants.WORK_QUEUE,null,msg.getBytes());

        //5.释放连接
        channel.close();
        connection.close();
    }
}
